package com.hxd.otc.module.convenience.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.ConvenienceBusinessTypeEnum;
import com.hxd.otc.module.convenience.adapter.OilHistoryAdapter;
import com.hxd.otc.module.convenience.adapter.OilRMBAdapter;
import com.hxd.otc.module.convenience.bean.ConvenienveHistoryBean;
import com.hxd.otc.module.convenience.bean.NorGoodsBean;
import com.hxd.otc.module.convenience.bean.OrderInfoBean;
import com.hxd.otc.module.convenience.bean.RefuelingServiceBean;
import com.hxd.otc.module.convenience.contract.OilRechargeContract;
import com.hxd.otc.module.convenience.fragment.PayDialogFragment;
import com.hxd.otc.module.convenience.presenter.OilRechargePresenter;
import com.hxd.otc.module.login.activity.LoginActivity;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.TitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 加油充值页面
 */
public class OilRechargeActivity extends BaseActivity<OilRechargePresenter> implements OilRechargeContract.View {

    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.rl_container)
    RelativeLayout rlContainer;

    @BindView(R.id.et_number)
    EditText etNumber;

    @BindView(R.id.et_phone)
    EditText etPhone;

    @BindView(R.id.et_name)
    EditText etName;

    @BindView(R.id.iv_clear)
    ImageView ivClear;

    @BindView(R.id.ll_history_numbe)
    LinearLayout llHistoryNumber;

    @BindView(R.id.rc_rmb)
    RecyclerView rcRmb;

    @BindView(R.id.rc_history_number)
    RecyclerView rcHistoryNumber;

    OilHistoryAdapter historyAdapter;
    OilRMBAdapter rmbAdapter;
    RefuelingServiceBean serviceBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_oil_recharge;
    }

    @Override
    protected void initView() {
        serviceBean = (RefuelingServiceBean) getIntent().getExtras().getSerializable("info");
        titleBar.setTitleText(serviceBean.getModuleTitle() + "充值");
        rcRmb.setLayoutManager(new GridLayoutManager(this, 3));
        rcHistoryNumber.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        etNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    rmbAdapter.setEnable(false);
                    ivClear.setVisibility(View.GONE);
                    if (etNumber.hasFocus()) {
                        llHistoryNumber.setVisibility(View.VISIBLE);
                    } else {
                        llHistoryNumber.setVisibility(View.GONE);
                    }
                    rmbAdapter.notifyDataSetChanged();
                    return;
                }

                rmbAdapter.setEnable(true);
                rmbAdapter.notifyDataSetChanged();
                ivClear.setVisibility(View.VISIBLE);
                llHistoryNumber.setVisibility(View.GONE);
            }
        });
        etNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (historyAdapter.getData().size() > 0 && TextUtils.isEmpty(etNumber.getText().toString()))
                        llHistoryNumber.setVisibility(View.VISIBLE);
                } else {
                    llHistoryNumber.setVisibility(View.GONE);
                }
            }
        });
    }

    PayDialogFragment payDialogFragment;

    private void initRMBAdapter() {
        if (rmbAdapter == null) {
            rmbAdapter = new OilRMBAdapter();
            rmbAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    pay(position);
                }
            });
        }
        rcRmb.setAdapter(rmbAdapter);
        rmbAdapter.setEnable(false);
    }

    OrderInfoBean orderInfoBean;

    private void pay(int position) {
        if (!UserHelper.isLogin()) {
            Bundle bundle = new Bundle();
            bundle.putBoolean(LoginActivity.EXTRA_FINISH_SKIP_MAIN, false);
            $startActivity(LoginActivity.class, bundle);
            return;
        }
        if (verification()) return;

        orderInfoBean = new OrderInfoBean();
        orderInfoBean.setGoodsId(rmbAdapter.getItem(position).getId());
//        orderInfoBean.setModuleId(rmbAdapter.getItem(position).getModuleId());
        orderInfoBean.setRealSymbol(rmbAdapter.getItem(position).getSymbol());
        orderInfoBean.setRechargeAccount(etNumber.getText().toString());
        orderInfoBean.setGasCardTel(etPhone.getText().toString());
        orderInfoBean.setGasCardName(etName.getText().toString());
        orderInfoBean.setVolume(rmbAdapter.getItem(position).getVolume());
        orderInfoBean.setModelTitle(rmbAdapter.getItem(position).getModuleTitle());

        orderInfoBean.setGoodsName(rmbAdapter.getItem(position).getGoodsName());
        orderInfoBean.setSellPrice(rmbAdapter.getItem(position).getSellPrice() + "");
        orderInfoBean.setBusinessType(ConvenienceBusinessTypeEnum.GAS);
        if (payDialogFragment == null) {
            payDialogFragment = PayDialogFragment.getInstance();
        } else {
            UIUtils.dismissDialogFragment(payDialogFragment);
        }
        payDialogFragment.setData(orderInfoBean);
        payDialogFragment.show(getSupportFragmentManager(), PayDialogFragment.class.getSimpleName());
    }

    private boolean verification() {
        if (TextUtils.isEmpty(etNumber.getText().toString())) {
            showToast(getString(R.string.enter_oil_card_number));
            return true;
        }
        if (TextUtils.isEmpty(etPhone.getText().toString())) {
            showToast(getString(R.string.please_input_phone_number));
            return true;
        }
        if (TextUtils.isEmpty(etName.getText().toString())) {
            showToast(getString(R.string.enter_name_hint));
            return true;
        }
        return false;
    }

    private void initHistoryNumberAdapter() {
        if (historyAdapter == null) {
            historyAdapter = new OilHistoryAdapter();
            historyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    etNumber.setText(historyAdapter.getItem(position).rechargeAccount);
                    llHistoryNumber.setVisibility(View.GONE);
                }
            });
        }
        rcHistoryNumber.setAdapter(historyAdapter);
    }

    @Override
    protected void initData() {
        initRMBAdapter();
        initHistoryNumberAdapter();
        mPresenter.onLoadRmb(serviceBean.getId());
        if (serviceBean.getModuleCode().equals("sinopec")) {//中国石化
            mPresenter.onLoadHistory("3");
        } else {//中国石油
            mPresenter.onLoadHistory("4");
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected OilRechargePresenter createPresenter() {
        return new OilRechargePresenter(this);
    }

    @Override
    public void onSetRMBAdapter(List<NorGoodsBean> list) {

        rmbAdapter.setNewData(list);
    }

    @Override
    public void onHistoryShowNoMore() {

    }

    @Override
    public void onSetHistoryAdapter(List<ConvenienveHistoryBean> list) {
        if (list == null || list.size() <= 0) {
            llHistoryNumber.setVisibility(View.GONE);
            return;
        }
        historyAdapter.setNewData(list);
    }

    @Override
    public void onRMBShowNoMore() {

    }

    @Override
    public void onDeleteSucced() {
        showToast(getString(R.string.delete_succeed));
        if (serviceBean.getModuleCode().equals("sinopec")) {//中国石化
            mPresenter.onLoadHistory("3");
        } else {//中国石油
            mPresenter.onLoadHistory("4");
        }
    }

    @OnClick({R.id.iv_clear, R.id.tv_clear_number, R.id.rl_container})
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.iv_clear:
                etNumber.setText("");
                llHistoryNumber.setVisibility(View.GONE);
                ivClear.setVisibility(View.GONE);
                break;
            case R.id.tv_clear_number:
            case R.id.rl_container:
                llHistoryNumber.setFocusable(true);
                llHistoryNumber.setVisibility(View.GONE);
                llHistoryNumber.setFocusableInTouchMode(true);
                if (historyAdapter.getData() != null && historyAdapter.getData().size() > 0) {
                    mPresenter.onDelete(historyAdapter.getItem(0).flagType + "");
                }
                break;

        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

}
